define(["jQuery","kendo","touchPan","niceScrollLib"], function ($,kendo,touchPan,niceScrollLib) {
	popup : null;
	//uplimg : null;
    return {
    	panActive : false,
		panOffset : {left:0,top:0},
        init: function (e) {
        	$(".minimize_parent").click(function(e){
        		$(this).hide();
        		$('.maximize_parent').show();
        		$("#page_thumbnails").hide();
				App.draw.updateDrawCanvasSize();
        	});
        	
        	$(".maximize_parent").click(function(e){
        		$(this).hide();
        		$('.minimize_parent').show();
        		$("#page_thumbnails").show();
				App.draw.updateDrawCanvasSize();
        	});
			
        	$(".ico-home").click(function(e){
        		App.goHome();
        	});
        	
        
        	
        	$(".reset-bg-tools").click(function(e){	
				App.clearBackground();
			});
        	$(".pan-tools").click(function(e){	
        		App.draw.panActive = !App.draw.panActive;
				//Firman : Pan Active
				if(App.draw.panActive){
					$(this).addClass('active');
				}else{
					$(this).removeClass('active');
				}
				//Firman : Pan Active
				App.test = App.draw.panActive;
				if(App.draw.panActive){
					if(App.isModeArea == 1)
						App.toggleMode(0);
					//Firman : Remove Old Script For Pan	
					/*if(navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i) || navigator.userAgent.match(/iPod/i)){
						App.draw.initPan();
					}else{
						$('.drawContainer').css({'cursor':'move'});
					}*/
					
        		}else{
        			$('.drawContainer').css({'cursor':'default'});
					//Firman : remove old script
					/*if(navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i) || navigator.userAgent.match(/iPod/i)){
						App.draw.endPan();
					}*/
        		}
        	});
			
			// if($(window).width()<1000){
				// $("#show-floating-menu-small").show('slow');
				// $("#header").hide('slow');
				// $('.drawContainer').height($(window).height());
			// }else{
				// $("#show-floating-menu-small").hide('slow');
				// $("#header").show('slow');
			// }
			
			// $(".small-menu-toggle").click(function(e){
				// if($('#show-floating-menu-small').css('display') == 'none'){ 
					// $('#floating-menu-small').hide('slow');
					// $('#show-floating-menu-small').show('slow');
				// }else{
					// $('#floating-menu-small').show('slow');
					// $('#show-floating-menu-small').hide('slow');
				// }
				
			// });
        	// $(window).resize(function() {
				// if(!App.stickyText.menu.isShow && !App.richText.menu.isShow){
					// $('.drawContainer').width($(window).width()-$('#page_thumbnails').width()-15);
					// if($(window).width()<1000){
						// $('.drawContainer').height($(window).height());
					// }
					// if(!(navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i) || navigator.userAgent.match(/iPod/i))){
						// $('.drawContainer, #page_thumbnails').height($(window).height()-$('.drawContainer').offset().top-50);
						// if($(window).width()<1000){
							// $("#show-floating-menu-small").show('slow');
							// $('#floating-menu-small').hide('slow');
							// $("#header").hide('slow');
						// }else{
							// $("#show-floating-menu-small").hide('slow');
							// $('#floating-menu-small').hide('slow');
							// $("#header").show('slow');
						// }
					// }
				// }
			// });
        	
			$(window).resize(function() {
				App.draw.updateDrawCanvasSize();
			});
			App.draw.updateDrawCanvasSize();
			// $('.drawContainer, #page_thumbnails').height($(window).height()-$('.drawContainer').offset().top-15);
        	
			if(true){//navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i) || navigator.userAgent.match(/iPod/i)){
				//Firman : Pan Function
				$('.drawContainer').niceScroll({autohidemode:false});
				//App.draw.initPan();
			}else{
			
				$('.drawContainer').on("mousedown touchstart", function (event) {
					
					if(App.draw.panActive){
						$(this)
							.data('down', true)
							.data('x', event.clientX)
							.data('scrollLeft', this.scrollLeft)
							.data('y', event.clientY)
							.data('scrollTop', this.scrollTop);
						
							
						return false;
					}
					}).on("mouseup touchend", function (event) {
						if(App.draw.panActive){
							$(this).data('down', false);
							App.draw.panOffset = {left:this.scrollLeft,top:this.scrollTop};
							//alert(App.draw.panOffset.left + "," + App.draw.panOffset.top);
						}
					}).on("mousemove touchmove", function (event) {
						if(App.draw.panActive){
							if ($(this).data('down') == true) {
								this.scrollLeft = $(this).data('scrollLeft') + $(this).data('x') - event.clientX;
								this.scrollTop = $(this).data('scrollTop') + $(this).data('y') - event.clientY;
								
							}
						}
					}).mousewheel(function (event, delta) {
						if(App.draw.panActive){
							this.scrollLeft -= (delta * 30);
							this.scrollTop -= (delta * 30);
							
						}
					}).css({
						'overflow' : 'hidden',
						'cursor' : 'default'
				});
			}
			
			$('.print-area-tools').live('click', function(e){
				var container = $(this).attr('rel'); 
				var my_window = false;
				my_window = window.open(App.baseUrl+"home/print_page?f_id="+App.fileId,"DigitalBoard");
				return false;
			});	 	
			
			
			
			$('.remove-pop').on('click',function(e){
				if(App.openedTool)
				App.openedTool.removeClass('active');
				App.creationKind = false;
				
			});
			
			$('.save-creation').on('click',function(e){
				App.creationKind = false;
			});
			
			$.each($('.colorbox span'), function(){
				$(this).on('click', function(){
					$('.colorbox span').removeClass('active');
					$(this).addClass('active');
					App.color = $(this).attr('data-color');
				});
			});
			
			$('canvas').live('click', function(e){
				switch(App.inserting){
					case 0:
					break;
					case 1:
						console.log('texting');
						var type, x, y, color, size, font, text;
						type = e.handleObj.type;
						color = App.color;
						size = $('#textsize option:selected').val();
						font = $('#textfont option:selected').val();
						text = $('#textoinsert').val();
						x = App.toCanvasX(e);
						y = App.toCanvasY(e);
						
						App.write(App.currentCtxId, x, y, type, color, size, font, text);
					break;
					default:
					break;
				}
			});

			$('#clear').live('click', function(data){
				for(var i = 0; i< App.currentCtxId; i++) {
					if(App.ctx[i])
					App.ctx[i].clearRect(0, 0, App.canvas.width, App.canvas.height);
				}
			});
			
			$('#inserttext').live('click', function(){
				App.inserting = 1;
				$.each($(document).find('input'), function(){
					$(this).attr("disabled", "disabled");
				});
				$('canvas').css('cursor', 'text');
			});
			
			$('#save').live('click', function(){
				console.log('saving...');
				for(var i = 0; i< App.currentCtxId; i++) {
					if(App.ctx[i])
						console.log(App.ctx[i].id);
				}
			});
			$('.menubox-small a, .toolbox-small a,#switch_mode_btn_small').click( function(){
					$('#floating-menu-small').hide('slow');
					$('#show-floating-menu-small').show('slow');
			});
			
			$("#dummy-button").click(function(){
				App.draw.endPan();
			});
			
			$('.tabViewArea').live('click', function(){  
				$(this).css('background-color','#F2F9FE');
				$('#editTabArea').css('background-color','#86BAE1'); 
				$('#rootContainer').children().each(function(){  
					if($(this).attr('is-template') != 1)
					{
						$(this).draggable('disable');
						$(this).resizable('disable');
						$(this).find("#"+$(this).attr('id')+"-c").attr("disabled", true);
						$(this).find("#segrip").attr("disabled", true);
						$(this).find("#ngrip").attr("disabled", true);
						$(this).find("#sgrip").attr("disabled", true);
						$(this).find("#egrip").attr("disabled", true);
						$(this).find("#wgrip").attr("disabled", true); 
						$(this).find("#segrip").css("display", "none");
						$(this).find("#ngrip").css("display", "none");
						$(this).find("#sgrip").css("display", "none");
						$(this).find("#egrip").css("display", "none");
						$(this).find("#wgrip").css("display", "none");
					}
				});
				App.isModeArea = 0;
			});
			// $('.toggle_mode_button').on('tap click',function(e){
				// if(App.isModeArea == 1)
				// {
					// $('#rootContainer').children().each(function(){  
						// if($(this).attr('is-template') != 1)
						// {
							// $(this).draggable('disable');
							// $(this).resizable('disable');
							// $(this).find("#"+$(this).attr('id')+"-c").attr("disabled", true);
							// $(this).find("#segrip").attr("disabled", true);
							// $(this).find("#ngrip").attr("disabled", true);
							// $(this).find("#sgrip").attr("disabled", true);
							// $(this).find("#egrip").attr("disabled", true);
							// $(this).find("#wgrip").attr("disabled", true); 
							// $(this).find("#segrip").css("display", "none");
							// $(this).find("#ngrip").css("display", "none");
							// $(this).find("#sgrip").css("display", "none");
							// $(this).find("#egrip").css("display", "none");
							// $(this).find("#wgrip").css("display", "none");
						// }
					// });
					// App.isModeArea = 0;
					// $('.toggle_mode_button').attr('value',App.localize("%edit_mode"));
				// }
				// else
				// {
					// $('#rootContainer').children().each(function(){
						// if($(this).attr('is-template') != 1)
						// {
							// if(App.getLockStatus($(this).attr("id")) != App.lockEnum.MOVEMENT)
							// $(this).draggable('enable');
							// $(this).resizable('enable');
							// $(this).find("#"+$(this).attr('id')+"-c").attr("disabled", false);
							// $(this).find("#segrip").attr("disabled", false);
							// $(this).find("#ngrip").attr("disabled", false);
							// $(this).find("#sgrip").attr("disabled", false);
							// $(this).find("#egrip").attr("disabled", false);
							// $(this).find("#wgrip").attr("disabled", false);	 
							// $(this).bind('click');
						// }
					// });
					// App.isModeArea = 1;
					// $('.drawContainer').css({'cursor':'default'});
					// App.draw.panActive=false;
					// $('.toggle_mode_button').attr('value',App.localize("%view_mode"));
				// }
			// });
			$('#toggle_mode_button').trigger('click');
		
			
			console.log('Drawer inited..');
		},
		
		updateDrawCanvasSize : function(){
			if(document.getElementById("page_thumbnails").style.display == "none"){
				$('.drawContainer').width($(window).width());
			}else{
				$('.drawContainer').width($(window).width()-$('#page_thumbnails').width());
			}
			
			if(document.getElementById("header").style.display == "none"){
				$('#page-list-container').height($(window).height()-15);
				$('.drawContainer').height($(window).height()-15);
			}else{
				$('#page-list-container').height($(window).height()-$('#header').height()-15);
				$('.drawContainer').height($(window).height()-$('#header').height()-15);
			}
		},
		
		initPan : function(){
			//alert($('#mainCanvas').width() + "," + $('#mainCanvas').height());
			$("<div></div>").attr('id','temp-pan').appendTo('#mainCanvas');    
			$('#temp-pan').append($('#pan-container'));
			$('#temp-pan').touchPanView({
				width:835,//$('#mainCanvas').width(),
				height:490//$('#mainCanvas').height()
			});
			//alert("OK");
		},
		
		endPan : function(){
			$('#mainCanvas').append($('#pan-container'));
			//Remove Pan Element
			$('#temp-pan').remove();
			$('.touchpanview-wrap').remove();
			$('.touchpanview-pan').remove();
		}
	}
	
	
});